{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    ">* list方法/函数 的所有使用场景及案例\n",
    ">> 1.append\n",
    ">> 2.pop(index)\\remove(values)\n",
    ">> 3.index()\n",
    ">> 4.extend()\n",
    ">> 5.count()\n",
    ">> 6.sort()\n",
    ">> ...\n",
    ">* list高级方法 如：\n",
    ">>1.copy() 复制 不等于赋值，复制可等价于 切片[:]\n",
    ">>2.enu\n",
    ">*课本练习 （课堂上老师带我们做了） Don't panic==>on tap,其中知识点：\n",
    ">>1.list的一些方法实现\n",
    ">>2.list的切片实现\n",
    ">>3.str=>list的方法，list(str)\n",
    ">>4.list=>str的方法, join（list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "vowels = ['a','e','i','o','u'] # 这是元音\n",
    "word = \"Milliways\"# 这是文本（str）\n",
    "# 需求：word中出现的vowels打印出来\n",
    "for letter in word: #letter 可以随便命名，不会影响我们的结果\n",
    "    print(letter)    #一般情况下，item（内容）=>简写：i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for i in word:  #for循环的第二个列表，决定了我们循环的内容\n",
    "    print(i)    #循环的列表，每个元素单独在一行打印\n",
    "                #"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "for i in word:\n",
    "    if i in vowels:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u'] \n",
    "word = \"Milliways\"\n",
    "for i in word: # for循环的第二个元素，决定了我们循环的内容\n",
    "    if i in vowels:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# append() 经常用于新建列表\n",
    "vowels = ['a','e','i','o','u'] # 这是元音\n",
    "word = \"Milliways\"# 这是文本（str）\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['youge', 'Mike', 'Marry', 'Jan', 'Jack', 'Alex', 'zhichao']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#插入insert（）可以指定位置增加元素\n",
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "names.append(\"Alex\")\n",
    "names.append(\"zhichao\")\n",
    "names.insert(0,\"youge\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['youge', 'Mike', 'youge', 'Marry', 'Jan', 'Jack', 'Alex']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除remove 和 pop remove()一般情况下 使用remove先用in做判断\n",
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "names.append(\"Alex\")\n",
    "names.append(\"zhichao\")\n",
    "names.insert(0,\"youge\")\n",
    "names.insert(2,\"youge\")\n",
    "names.remove(\"zhichao\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素的个数: 4\n"
     ]
    }
   ],
   "source": [
    "#len()查看list长度\n",
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "print(\"names的长度/names的列表元素的个数:\", len(names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "#index索引 查看该元素如1001在stu_id中第一个出现的位置索引\n",
    "stu_id =[1001,1002,1003,1004,1008,1001,1009,1020,1001]\n",
    "print(stu_id.index(1001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "#clear()清除所有数据\n",
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]\n",
    "names.clear()\n",
    "print (\"清除names列表后的值：\",names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "#count()查看列表元素出现的个数，（简单应用）统计分数出现的次数\n",
    "stu_id =[1001,1002,1003,1004,1008,1001,1009,1020,1001]\n",
    "print(stu_id.count(1001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1001, 1002, 1003, 1004, 1008, 1001, 1009, 1020, 1001]\n"
     ]
    }
   ],
   "source": [
    "#extend()是给指定的list进行扩充\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1001\n",
      "5 1001\n",
      "8 1001\n",
      "我们想要删除pop（）的值： [0, 5, 8]\n"
     ]
    }
   ],
   "source": [
    "stu_id =[1001,1002,1003,1004,1008,1001,1009,1020,1001]\n",
    "\n",
    "#for i in stu_id:\n",
    "        #循环遍历stu_id的所有内容\n",
    "        # if i == 1001:\n",
    "                #判断如果元素为1001的时候，怎么取出索引呢？\n",
    "           # print(i)\n",
    "#因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，因此，有了枚举的方法，同时遍历两遍    \n",
    "#enumerate()\n",
    "pop_values_list = []\n",
    "for index,item in enumerate(stu_id):\n",
    "    if item == 1001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop（）的值：\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**购物车程序项目：\n",
    "\n",
    "　　要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   　　2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  　　 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的钱120\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-f714494fba09>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mproduct_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         print(\"编号%s,商品名称：%s,商品价格：%s元\"\n\u001b[1;32m----> 9\u001b[1;33m         %(i,product_list[i][0],product_list[i][1]))\n\u001b[0m\u001b[0;32m     10\u001b[0m         \u001b[0mchoice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"请输入购买编号:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mchoice\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misdigit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not list"
     ]
    }
   ],
   "source": [
    "product_list = [[\"苹果\",10],\n",
    "                [\"草莓\",25],\n",
    "                [\"口红\",70],\n",
    "                [\"裙子\",100],]\n",
    "user_shopping_list = []\n",
    "Alipay = int(input(\"请输入您的钱\"))\n",
    "for i in product_list:\n",
    "        print(\"编号%s,商品名称：%s,商品价格：%s元\"\n",
    "        %(i,product_list[i][0],product_list[i][1]))\n",
    "        choice = input(\"请输入购买编号:\")\n",
    "if choice.isdigit():\n",
    "    choice_int = int(choice)\n",
    "    if choice<len(product_list):\n",
    "          if product_list[choice][1]<Alipay:\n",
    "            user_shopping_list.append(product_list[choice])\n",
    "            Alipay = Alipay-product_list[choice][1]\n",
    "            print(\"您的商品已添加成功，已添加到购物车，您的余额还有%s\"%(Alipay))\n",
    "    else:\n",
    "            print(\"您的余额不足，可用花呗支付~\") \n",
    "elif choice == \"q\":\n",
    "          print(\"用户购物车:\",user_shopping_list)\n",
    "          print(\"您的余额还有:\",Alipay)\n",
    "break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：12000\n",
      "------goods list-------\n",
      "['1', 'python课本', '88']\n",
      "['2', '蜜雪冰城—冰茶', '10']\n",
      "['3', 'iphone13 pro', '9999']\n",
      "['4', '华为电-跑车', '400000']\n",
      "['5', '小米手环', '199']\n",
      "\n",
      "请输入商品的ID：2\n",
      "['1', 'python课本', '88']\n",
      "您输入的商品信息有问题，请输入正确的商品ID\n",
      "['2', '蜜雪冰城—冰茶', '10']\n",
      "[['2', '蜜雪冰城—冰茶', '10']]\n"
     ]
    }
   ],
   "source": [
    "goods = [\n",
    "    [\"1\",\"python课本\",\"88\"],\n",
    "    [\"2\",\"蜜雪冰城—冰茶\",\"10\"],\n",
    "    [\"3\",\"iphone13 pro\",\"9999\"],\n",
    "    [\"4\",\"华为电-跑车\",\"400000\"],\n",
    "    [\"5\",\"小米手环\",\"199\"]\n",
    "]\n",
    "user_shop_list = []\n",
    "\n",
    "支付宝余额 = input(\"请输入您的余额：\")\n",
    "info = '''------goods list-------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(goods[0],goods[1],goods[2],goods[3],goods[4])\n",
    "print(info)\n",
    "user_input_ID = input(\"请输入商品的ID：\")\n",
    "for good in goods:\n",
    "    print(good)\n",
    "    # 判断商品的id是否正确\n",
    "    if user_input_ID == good[0]:\n",
    "        # 判断用户的余额是否够用\n",
    "        if int(good[2]) < int(支付宝余额):\n",
    "            # 把商品加进list\n",
    "            user_shop_list.append(good)\n",
    "            # 扣除支付宝余额\n",
    "            break\n",
    "\n",
    "    else:\n",
    "        print(\"您输入的商品信息有问题，请输入正确的商品ID\")\n",
    "\n",
    "print(user_shop_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
